<template>
  <div class="login-cont">
    <div class="artwork"><img src="../assets/loginPic.png" alt=""></div>
    <div class="main">
      <div class="login-box">
        <loginLogo></loginLogo>
        <div class="login-box-bottom">
          <div class="login-form">
            <el-form ref="form" :model="loginForm" :rules="rules" label-position="left" label-width="0">
              <el-form-item prop="username">
                <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="do_login" :loading="loading">登 录</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div class="login-hint">
            <ul>
              <li><el-link type="info" @click="$router.push('/signup')">没有账号，现在注册</el-link></li>
              <li><el-link type="info" @click="$router.push('/forget')">忘记密码，立刻找回</el-link></li>
            </ul>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import loginLogo from '../components/LoginLogo.vue'
import { setToken } from '@/utils/auth'
import { login } from '@/api/login'

export default {
  components: {
    loginLogo,
  },
  data() {
    return {
        loginForm: {
          username: '',
          password: ''
        },
        rules: {
          username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
          password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
        },
        loading: false
      }
  },
  methods: {
    do_login() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true
            //发起网络请求，登录；如果登录成功，跳转到主页面
             login(this.loginForm.username, this.loginForm.password).then(res =>{
              setToken(res.data.token)
              console.log(res.data.token)
              this.$router.push({ path: '/main/home' })
            }).catch(() => {
              // 登录失败，显示错误提示
              this.$message.error('用户名或密码错误')
              this.loading = false
            })
          }
        })
      }

  }
}



</script>

<style lang="scss">
.login-cont {

  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;

  .artwork {
    width: 514px;
    height: 100%;
    background-color: #FFEACE;
    position: relative;

    img {
      position: absolute;
      top: 50%;
      left: 50%;
      margin-top: -250px;
      margin-left: -330px;
    }
  }

  .main {
    flex: 1;
    height: 100%;
    background-color: #FFF7EF;
    position: relative;

    .login-box {
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -210px;
      margin-top: -160px;
      width: 500px;

      .login-box-bottom {
        display: flex;
        justify-content: space-between;
      }

      .login-form {
        float: left;
        margin-top: 35px;
        width: 350px;

        .el-input__inner {
          background-color: #FFFEFC;
          width: 350px;
          border: none;
          color: #865E2A;
          font-size: 18px;
          padding: 10px;
          border-radius: 5px;
        }

        .el-input__inner:focus {
          border: solid 2px #402E32;
        }

        .el-input__inner::placeholder {
          color: #BAA89B;
          font-size: 18px;
        }

        .el-button--primary {
          width: 350px;
          background-color: #402E32;
          border: none;
          font-size: 18px;
          color: #FFFEFC;
        }
      }

      .login-hint {
        float: right;
        font-size: 15px;
        margin-top: 35px;

        ul li {
          height: 18px;
          padding-left: 10px;
          padding-top: 22px;
          margin-bottom: 24px;
        }

        .el-link {
          color: #402E32 !important;
          width: 120%;
          margin-top: -20%;
        }
      }
    }
  }

  a:hover {
    text-decoration: none;
  }

}
</style>